package chapter14.web;

import java.util.*;

import chapter14.core.*;

import chapter14.domain.*;

import chapter14.manager.*;

import com.softwarementors.extjs.djn.config.annotations.DirectFormPostMethod;
import com.softwarementors.extjs.djn.config.annotations.DirectMethod;

import org.apache.commons.fileupload.FileItem;


public class ProductAction extends SecurityAction<Product> {
    private ProductManager productManager;

    @Override
    protected ProductManager getEntityDao() {
        return productManager;
    }

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

    @Override
    @DirectFormPostMethod
    public Object save(Map<String, String> param,
        Map<String, FileItem> files) throws Exception {
        if (!this.check("Product")) {
            return this.failure();
        }

        return super.save(param, files);
    }

    @Override
    @DirectMethod
    public Object pagedQuery(int start, int limit, String sort,
        String dir, String query) throws Exception {
        if (!this.check("Product")) {
            return this.failure();
        }

        return super.pagedQuery(start, limit, sort, dir, query);
    }

    @DirectMethod
    @Override
    public Object remove(String ids) throws Exception {
        if (!this.check("Product")) {
            return this.failure();
        }

        return super.remove(ids);
    }

    @DirectMethod
    Object stat(int start, int limit, String sort, String dir,
        String query, String endDateStr) throws Exception {
        if (!this.check("Stat")) {
            return this.failure();
        }

        Date endDate = this.getDate(endDateStr, new Date());
        String hql = "from Product where 1=1";
        List params = new ArrayList();

        if (this.notEmpty(query)) {
            hql += " and name like ?";
            params.add("%" + query + "%");
        }

        //if (endDate != null) {
        //    // TODO:
        //    hql += " and createTime<=?";
        //    params.add(endDate);
        //}
        if (notEmpty(sort) && notEmpty(dir)) {
            hql += (" order by " + sort + " " + dir);
        }

        int pageNo = (start / limit) + 1;
        int pageSize = limit;

        Page page = productManager.pagedQuery(hql, pageNo, pageSize,
                params.toArray());
        List result = new ArrayList();
        List<Product> productList = (List<Product>) page.getResult();

        for (Product p : productList) {
            String inHql = "select sum(o.quantity) from InList o where o.product=? and o.in.createTime<=?";
            int in = productManager.getCount(inHql, p, endDate);
            String outHql = "select sum(o.quantity) from OutList o where o.product=? and o.out.createTime<=?";
            int out = productManager.getCount(outHql, p, endDate);
            int stock = in - out;
            Map map = new HashMap();
            map.put("id", p.getId());
            map.put("name", p.getName());
            map.put("spec", p.getSpec());
            map.put("price", p.getPrice());
            map.put("in", in);
            map.put("out", out);
            map.put("stock", stock);
            result.add(map);
        }

        page.setResult(result);

        return page;
    }
}
